#! /usr/bin/python3
# encoding : utf-8

from DataGen.DataStruct import PersonData
from DataGen.DataStruct import MedicalRecordData
from DataGen.DataStruct import FileMateData
from faker import Faker
from DataGen.Date import Date
import random


def DataGenerator(n=100):
    persons = _PersonDataGenerator(n)
    recodes = _MedicalRecordDataGenerator(persons, 10 * n)
    files = _FileMateDataGenerator(recodes, 100 * n)
    return persons, recodes, files


def _PersonDataGenerator(n=100):
    infos = []
    fake = Faker("zh_CN")
    for i in range(n):
        person = PersonData()
        person.Uid = i
        person.Name = fake.name()
        person.Birthday = Date.RandomDate().ToString()
        person.Phone = fake.phone_number()
        add = fake.address().split(' ')
        person.Address = add[0]
        person.Postcode = add[1]
        person.BodyHeight = round(random.uniform(100, 200), 2)
        person.BodyWeight = round(random.uniform(18, 30) * person.BodyHeight * person.BodyHeight / 100 / 100, 2)
        person.Remark = _RandomHeathStatus()
        person.MedicalRecords = []
        infos.append(person)
    return infos


def _MedicalRecordDataGenerator(person_list, n=1000):
    infos = []
    for i in range(n):
        record = MedicalRecordData()
        record.Uid = i
        patient = random.choice(person_list)
        record.PatientUid = patient.Uid
        patient.MedicalRecords.append(record.Uid)
        record.DiagnosisDate = Date.RandomDate(from_string=patient.Birthday).ToString()
        record.DiagnosisResultCode, record.DiagnosisResult = _RandomDiagnosisResult()
        record.FileList = []
        infos.append(record)
    return infos


def _FileMateDataGenerator(recode_list, n=10000):
    infos = []
    for i in range(n):
        file = FileMateData()
        k1, k2, k3, k4 = _RandomFileMate()
        file.Uid = i
        file.Type = k1
        file.Name = k2
        file.Desc = k4
        file.Url = "/{}/{}_{}.{}".format(k1, k2, i, k3)
        recode = random.choice(recode_list)
        file.RecodeUid = recode.Uid
        recode.FileList.append(file.Uid)
        infos.append(file)
    return infos


_HeathStatus = [
    "过敏",
    "轻度抑郁",
    "精神疾病",
    "吸烟过度",
    "酗酒",
    "素食主义",
    "生活不规律",
    "焦虑",
    "脱发",
    "曾受重伤",
    "残疾",
    "弱视"
]


def _RandomHeathStatus():
    num = random.randint(0, 3)
    string = ""
    for i in range(num):
        string = string + random.choice(_HeathStatus) + ","
    string = string[:-1]
    return string


_DiagnosisResults = {
    "0001": "心脏病",
    "0002": "高血压",
    "0003": "胆结石",
    "0004": "脑梗塞",
    "0005": "骨折",
    "0006": "心律不齐",
    "0007": "肺结核",
    "0008": "组织坏死",
    "0009": "造血功能异常"
}


def _RandomDiagnosisResult():
    n = random.randint(1, 9)
    key = "000{}".format(n)
    return key, _DiagnosisResults[key]


_FileInfo = {
    "Image": [["X光", "B超", "核磁共振"], ["jpg", "png"]],
    "Video": [["内窥镜"], "mp4"],
    "Table": [["血检单", "尿检单"], ["txt", 'exl']]
}


def _RandomFileMate():
    key = random.choice(["Image", "Video", "Table"])
    value = _FileInfo[key]
    desc = "不健康"
    if bool(random.randint(0, 1)):
        desc = "健康"
    return key, random.choice(value[0]), random.choice(value[1]), desc
